/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.flow.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.flow.entity.ClosedHazardGoodsStockEntity;
import com.hopes.bizz.close.flow.dto.ClosedHazardGoodsStockExcelDTO;
import com.hopes.bizz.close.flow.vo.ClosedHazardGoodsStockExcelVO;
import com.hopes.bizz.close.flow.service.ClosedHazardGoodsStockService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 企业危化品获取库存
 *
 * @author pig
 * @date 2024-02-06 10:00:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/stock" )
@Tag(description = "stock" , name = "企业危化品获取库存管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedHazardGoodsStockController {

    private final ClosedHazardGoodsStockService closedHazardGoodsStockService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param closedHazardGoodsStock 企业危化品获取库存
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('flow_stock_view')" )
    public R getClosedHazardGoodsStockPage(Page page, ClosedHazardGoodsStockEntity closedHazardGoodsStock) {
        LambdaQueryWrapper<ClosedHazardGoodsStockEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedHazardGoodsStock.getOrgId()), ClosedHazardGoodsStockEntity::getOrgId, closedHazardGoodsStock.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedHazardGoodsStock.getGoodsId()), ClosedHazardGoodsStockEntity::getGoodsId, closedHazardGoodsStock.getGoodsId());
        wrapper.like(StrUtil.isNotBlank(closedHazardGoodsStock.getStashName()), ClosedHazardGoodsStockEntity::getStashName, closedHazardGoodsStock.getStashName());
        wrapper.orderByAsc(ClosedHazardGoodsStockEntity::getOrgId).orderByDesc(ClosedHazardGoodsStockEntity::getDataTime);
        return R.ok(closedHazardGoodsStockService.page(page, wrapper));
    }


    /**
     * 通过id查询企业危化品获取库存
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('flow_stock_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(closedHazardGoodsStockService.getById(id));
    }

    /**
     * 新增企业危化品获取库存
     * @param closedHazardGoodsStock 企业危化品获取库存
     * @return R
     */
    @Operation(summary = "新增企业危化品获取库存" , description = "新增企业危化品获取库存" )
    @SysLog("新增企业危化品获取库存" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('flow_stock_add')" )
    public R save(@RequestBody ClosedHazardGoodsStockEntity closedHazardGoodsStock) {
        return R.ok(closedHazardGoodsStockService.save(closedHazardGoodsStock));
    }

    /**
     * 修改企业危化品获取库存
     * @param closedHazardGoodsStock 企业危化品获取库存
     * @return R
     */
    @Operation(summary = "修改企业危化品获取库存" , description = "修改企业危化品获取库存" )
    @SysLog("修改企业危化品获取库存" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('flow_stock_edit')" )
    public R updateById(@RequestBody ClosedHazardGoodsStockEntity closedHazardGoodsStock) {
        return R.ok(closedHazardGoodsStockService.updateById(closedHazardGoodsStock));
    }

    /**
     * 通过id删除企业危化品获取库存
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除企业危化品获取库存" , description = "通过id删除企业危化品获取库存" )
    @SysLog("通过id删除企业危化品获取库存" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('flow_stock_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedHazardGoodsStockService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('flow_stock_export')")
    public List<ClosedHazardGoodsStockExcelVO> template() {
        return closedHazardGoodsStockService.exportExcel(Collections.singletonList(new ClosedHazardGoodsStockEntity()));
    }

    /**
     * 导出excel 表格
     * @param closedHazardGoodsStock 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('flow_stock_export')")
    public List<ClosedHazardGoodsStockExcelVO> export(ClosedHazardGoodsStockEntity closedHazardGoodsStock) {
        LambdaQueryWrapper<ClosedHazardGoodsStockEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjUtil.isNotNull(closedHazardGoodsStock.getOrgId()), ClosedHazardGoodsStockEntity::getOrgId, closedHazardGoodsStock.getOrgId());
        wrapper.eq(ObjUtil.isNotNull(closedHazardGoodsStock.getGoodsId()), ClosedHazardGoodsStockEntity::getGoodsId, closedHazardGoodsStock.getGoodsId());
        wrapper.like(StrUtil.isNotBlank(closedHazardGoodsStock.getStashName()), ClosedHazardGoodsStockEntity::getStashName, closedHazardGoodsStock.getStashName());
        wrapper.orderByAsc(ClosedHazardGoodsStockEntity::getOrgId).orderByDesc(ClosedHazardGoodsStockEntity::getDataTime);
        return closedHazardGoodsStockService.exportExcel(closedHazardGoodsStockService.list(wrapper));
    }

    /**
     * 导入excel 表格
     * @param closedHazardGoodsStockExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('flow_stock_export')")
    public R importExcel(@RequestExcel List<ClosedHazardGoodsStockExcelDTO> closedHazardGoodsStockExcelDtoList, BindingResult bindingResult) {
        return closedHazardGoodsStockService.importExcel(closedHazardGoodsStockExcelDtoList, bindingResult);
    }
}
